package com.heba.greenmulu.scan;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.heba.Cfg;
import com.heba.bean.product.ItemBusinessAccountObject;
import com.heba.config.CommonConfig;
import com.heba.bean.product.ItemProductDetailObject;
import com.heba.bean.PhotoObject;
import com.heba.event.MessageEvent;
import com.heba.greenmulu.BaseActivity;
import com.heba.greenmulu.R;
import com.heba.helper.HttpClientHelper;
import com.heba.helper.JSONHttpHelper;
import com.heba.utils.DensityUtils;
import com.heba.utils.UniversalImageLoadTool;
import com.loopj.android.http.RequestParams;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 显示产品品牌成分信息
 * <p>
 * Created by jason on 2019/2/27.
 */

public class ItemProductInfoActivity extends BaseActivity {

    private EditText edit_barcode_num;
    private EditText edit_brand_name;
    private EditText edit_product_name;
    private EditText edit_weight;
    private EditText edit_ingredients;
    private EditText edit_remark1;
    private EditText edit_remark2;
    private LinearLayout linear_photo_view;
    private Button btn_submit;

    private ItemProductDetailObject itemProductDetailObject = new ItemProductDetailObject();
    private String productID = "";
    private String productName = "";
    private ItemBusinessAccountObject itemBusinessAccountObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_scan_product_report_error);
        initTopbar();
        findViewById();
        initView();
        getProductDetail();
    }

    @Override
    protected void findViewById() {
        edit_barcode_num = findViewById(R.id.edit_barcode_num);
        edit_brand_name = findViewById(R.id.edit_brand_name);
        edit_product_name = findViewById(R.id.edit_product_name);
        edit_weight = findViewById(R.id.edit_weight);
        edit_ingredients = findViewById(R.id.edit_ingredients);
        edit_remark1 = findViewById(R.id.edit_remark1);
        edit_remark2 = findViewById(R.id.edit_remark2);
        linear_photo_view = findViewById(R.id.linear_photo_view);
        btn_submit = findViewById(R.id.btn_submit);

    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            productID = bundle.getString(CommonConfig.PRODUCT_ID, "");
            productName = bundle.getString(CommonConfig.PRODUCT_NAME, "");
        }
        txt_title.setText(getResources().getString(R.string.product));

        /**
         * 禁止输入字符
         */
        edit_barcode_num.setFocusable(false);
        edit_barcode_num.setFocusableInTouchMode(false);
        edit_barcode_num.setEnabled(false);
        edit_brand_name.setEnabled(false);
        edit_product_name.setEnabled(false);
        edit_weight.setEnabled(false);
        edit_ingredients.setEnabled(false);
        edit_remark1.setEnabled(false);
        edit_remark2.setEnabled(false);

        btn_submit.setVisibility(View.INVISIBLE);
    }

    /**
     * 显示数据
     *
     * @param productObject
     */
    private void showProductData(ItemProductDetailObject productObject) {
        if (null != productObject) {
            edit_barcode_num.setText(productObject.getBarcode());
            edit_brand_name.setText(productObject.getBrandName());
            edit_product_name.setText(productObject.getProductName());
            edit_ingredients.setText(productObject.getIngredients());
            edit_weight.setText(productObject.getServingValuePer());
            edit_remark1.setText(productObject.getRemark1());
            edit_remark2.setText(productObject.getRemark2());
        }
    }

    /**
     * 显示图片
     */
    private void updateImageUploadView(List<PhotoObject> list) {
        linear_photo_view.removeAllViews();
        for (int i = 0; i < list.size(); i++) {
            linear_photo_view.addView(updateImageUploadRowView(list.get(i)));
        }
    }

    /**
     * updateImageUploadRowView
     *
     * @return view
     */
    private View updateImageUploadRowView(PhotoObject object) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_round_image, null);
        ImageView img_photo = view.findViewById(R.id.img_photo);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((DensityUtils.getWidth(this) - DensityUtils.dip2px(this, 20)) / 4,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        UniversalImageLoadTool.disPlay(object.getImageUrl(), img_photo, R.drawable.btn_upload_image);
        return view;
    }

    /**
     * 获取产品详情资料
     */
    private void getProductDetail() {
        progressDialog.show();
        HttpClientHelper client = new HttpClientHelper();
        client.setUseLogin(true);
        RequestParams params = new RequestParams();
        params.put("Leng", Cfg.Leng);
        params.put("ProductID", productID);
        client.get(Cfg.BaseUrl + "/Product/Detail", params,
                new JSONHttpHelper.JSONHttpResponseHandler(this) {

                    @Override
                    public void Success() {
                        // TODO Auto-generated method stub
                        super.Success();

                        progressDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(response.trim()).getJSONObject("Result");
                            updateProductData(jsonObject);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    public void Failure() {
                        super.Failure();
                        progressDialog.dismiss();
                    }

                });
    }

    /**
     * updateProductData
     *
     * @param object
     */
    private void updateProductData(JSONObject object) {
        try {
            itemProductDetailObject.setProductID(object.getString("ProductID"));
            itemProductDetailObject.setBarcode(object.getString("Barcode"));
            itemProductDetailObject.setBrandName(object.getString("BrandName"));
            itemProductDetailObject.setProductName(object.getString("ProductName"));
            itemProductDetailObject.setServingValuePer(object.getString("ServingValuePer"));
            itemProductDetailObject.setIngredients(object.getString("Ingredients"));
            itemProductDetailObject.setRemark1(object.getString("Remark1"));
            itemProductDetailObject.setRemark2(object.getString("Remark2"));
            if (object.has("ProductImageList")) {
                JSONArray array = object.getJSONArray("ProductImageList");
                List<PhotoObject> photoObjects = new ArrayList<>();
                if (array.length() > 0) {
                    for (int j = 0; j < array.length(); j++) {
                        JSONObject object1 = array.getJSONObject(j);
                        PhotoObject photoObject = new PhotoObject();
                        photoObject.setImageUrl(object1.getString("ImageUrl"));
                        photoObjects.add(photoObject);
                    }
                    itemProductDetailObject.setPhotoObjects(photoObjects);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        /**
         * 显示数据到界面上
         */
        showProductData(itemProductDetailObject);
        updateImageUploadView(itemProductDetailObject.getPhotoObjects());

    }

}
